import Editor from "../editor";
import Operation from "./operation";
import SetSelectionOperation from "./selection_operation_set";
import InsertTextOperation from "./text_operation_insert";

export class RemoveTextOperation extends Operation {
  _spacers: string = "";
  _startIndex: number;
  _endIndex: number;

  constructor(startIndex: number, endIndex: number) {
    super();
    this._startIndex = startIndex;
    this._endIndex = endIndex;
  }

  apply(editor: Editor): void {
    this._spacers = editor.getTextModel()?.getSpacersByIndex(this._startIndex, this._endIndex) || "";
    editor.getTextModel()?.remove(this._startIndex, this._endIndex);
  }

  reverse(): Operation[] {
    return [
      new InsertTextOperation(this._spacers, this._startIndex),
      new SetSelectionOperation({ 
        anchor: this._spacers.length === 1 ? this._startIndex + this._spacers.length : this._startIndex,
        focus: this._startIndex + this._spacers.length
      })
    ];
  }
}

export default RemoveTextOperation;